//
//  TBaseViewController.m
//  lianganyuchu
//
//  Created by iMac on 17/2/20.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "TBaseViewController.h"

@implementation UIView (FindFirstResponder)

- (UIView *)findFirstResponder {
    if (self.isFirstResponder) return self;
    for (UIView *subView in self.subviews) {
        UIView *firstResponder = [subView findFirstResponder];
        if (firstResponder != nil) return firstResponder;
    }
    return nil;
}

@end

@interface TBaseViewController ()
{
    CGFloat _maxY;
}
@end

@implementation TBaseViewController
-(void)viewWillAppear:(BOOL)animated
{
    [_naviVC setNaviHidden:NO];
    [_naviVC setCommonNaviVC];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    _naviVC=(YCNavigationViewController *)self.navigationController;
    
    progressbar = [[MBProgressHUD alloc] initWithView:self.view];
    progressbar.label.font=[UIFont LightFontOfSize:15];
    progressbar.label.text = @"正在加载...";
    [self.navigationController.view addSubview:progressbar];
    
    m_AlertView=[[YCAlertView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, kMainScreen_Height)];
    
    self->apiClient = [[ApiClient alloc]
                      init:^(ApiRequest *data, NSString *url, BOOL hideProgress) {
                          if (hideProgress) {
                              [progressbar hideAnimated:YES];
                          } else {
                              [progressbar showAnimated:YES];
                          }
                      }
                      afterRequest:^(ApiResponse *data, NSString *url) {
                          [progressbar hideAnimated:YES];
                          if (data.success.intValue==0) {
                              if (data.code.intValue==401) {
                                  [[App shared] setUser:nil];
                                  [[App shared]save];
                                  [[AppDelegate appDelegate]loginOut];
                              }else{
                                  [self.view showHUD:data.message];
                              }
                          }
                      }
                      getToken:^NSString * {
                          return [App shared].user.access_token;
                      }
                      getApiUrl:^NSString * {
                          return [NSString stringWithFormat:@"%@/", API_URL];
                      }];

}
- (void)showNavigationView:(UIViewController *)controller
{
    controller.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:controller animated:YES];
}

- (UIBarButtonItem *)tbarBackButton {
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(-10, 0, 10,18);
    [backBtn setImage:[UIImage imageNamed:@"arrow_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    return backItem;
}

- (UIBarButtonItem *)tbarBackButtonWhite {
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(-10, 0, 10,18);
    [backBtn setImage:[UIImage imageNamed:@"arrow_backwhite"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    return backItem;
}

- (void)goBack
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)pushToNavigationView:(UIViewController *)controller
{
    controller.hidesBottomBarWhenPushed=YES;
    [self.navigationController pushViewController:controller animated:YES];
}
- (void)textFieldDone {
    [[self.view findFirstResponder] resignFirstResponder];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (UIView *)findFirstResponderForView:(UIView *)view
{
    if (view.isFirstResponder) {
        return view;
    }
    if (view.subviews.count > 0) {
        
        for (UIView *obj in view.subviews) {
            UIView *firstResponder = [self findFirstResponderForView:obj];
            if (firstResponder != nil) {
                return firstResponder;
            }
        }
    }
    
    return nil;
}

@end






